package cn.reinforce.util.aliyun;

import com.aliyun.opensearch.OpenSearchClient;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.imm.model.v20170906.CreateOfficeConversionTaskRequest;
import com.aliyuncs.imm.model.v20170906.CreateOfficeConversionTaskResponse;
import com.aliyuncs.imm.model.v20170906.GetOfficeConversionTaskRequest;
import com.aliyuncs.imm.model.v20170906.GetOfficeConversionTaskResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 用于阿里云产品的操作，单例模式
 *
 * @author Fate
 * @date 2017/3/20
 */
public enum Aliyun {
    INSTANCE;

    private static final Logger LOG = LoggerFactory.getLogger(Aliyun.class.getName());

    private String accessKeyId;
    private String accessKeySecret;


    public String getAccessKeyId() {
        return accessKeyId;
    }

    public String getAccessKeySecret() {
        return accessKeySecret;
    }

    //OSS部分
    private String ossRegion;
    private boolean intranet;
    private String ossEndpoint;

    /**
     * 初始化Key和Secret
     *
     * @param accessKeyId
     * @param accessKeySecret
     */
    public void init(String accessKeyId, String accessKeySecret) {
        this.accessKeyId = accessKeyId;
        this.accessKeySecret = accessKeySecret;
    }

    /**
     * 初始化OSS配置
     *
     * @param ossRegion OSS地址
     * @param intranet
     */
    public void initOSS(String ossRegion, boolean intranet) {
        this.ossRegion = ossRegion;
        this.intranet = intranet;
        this.ossEndpoint = "http://oss-" + ossRegion + (intranet ? "-internal" : "") + ".aliyuncs.com";
    }

    public OSS getOSSClient() {
        return new OSSClientBuilder().build(ossEndpoint, accessKeyId, accessKeySecret);
    }

    public String getOssEndpoint() {
        return ossEndpoint;
    }

    public String getOssRegion() {
        return ossRegion;
    }

    public boolean isIntranet() {
        return intranet;
    }

    // open search
    private String appName;
    private OpenSearch openSearch;
    private OpenSearchClient openSearchClient;

    /**
     * 初始化OpenSearch
     *
     * @param endpoint URL
     * @param appName  应用名称
     */
    public void initOpenSearch(String endpoint, String appName) {
        this.appName = appName;
        this.openSearch = new OpenSearch(accessKeyId, accessKeySecret, endpoint);
        this.openSearchClient = new OpenSearchClient(openSearch);
    }

    public OpenSearchClient getOpenSearchClient() {
        return openSearchClient;
    }

    public String getAppName() {
        return appName;
    }

    //sms
    private IAcsClient smsClient;

    public void initSms() {
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "Sms", "sms.aliyuncs.com");
        smsClient = new DefaultAcsClient(profile);
    }

    //imm
    private String immOssEndpoint;
    private IAcsClient immClient;

    public void initImm(String immOssEndpoint) {
        this.immOssEndpoint = immOssEndpoint;
        immClient = new DefaultAcsClient(DefaultProfile.getProfile(ossRegion, accessKeyId, accessKeySecret));
    }

    public IAcsClient getImmClient() {
        return immClient;
    }

    public String getImmOssEndpoint() {
        return immOssEndpoint;
    }
}
